package com.ourcraft.informationcollectionsystem.controller.province;

import com.alibaba.fastjson.JSONObject;
import com.ourcraft.informationcollectionsystem.service.province.dataFormExamine.DataFormExamineService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * DataFormExamineController handles dataReportForm-examine-related operations.
 *
 * @author Liu Yiming
 * @since 2023-11-04
 */
@RequestMapping("province/dataFormExamine/")
@RestController
@CrossOrigin()
@AuthorityValidator(code = AuthorityCode.DATA_FORM_EXAMINE)
public class DataFormExamineController {

    @Autowired
    DataFormExamineService dataFormExamineService;

    /**
     * Retrieves information of all forms out by user ID.
     *
     * @param userId The ID of the user.
     * @param request Permission verification related.
     * @return information of all forms.
     */
    @GetMapping("getAllForms")
    public Result<ArrayList<Map<String, Object>>> getAllForms(@RequestParam int userId, HttpServletRequest request)
    {
        ArrayList<Map<String, Object>> result = dataFormExamineService.getAllRecords(userId);
        return Result.success(result);
    }

    /**
     * pass the form in request.
     *
     * @param data The form to be passed.
     * @param request Permission verification related.
     * @return Operation result.
     */
    @PostMapping("passForm")
    public Result<Boolean> passRecord(@RequestBody JSONObject data, HttpServletRequest request)
    {
        // 从data中获取formId
        int formID = data.getIntValue("formID");
        boolean success = dataFormExamineService.passForm(formID);
        if (success) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * return back the form in request.
     *
     * @param data The form to be returned.
     * @param request Permission verification related.
     * @return Operation result.
     */
    @PostMapping("returnBackForm")
    public Result<Boolean> returnBackRecord(@RequestBody JSONObject data, HttpServletRequest request)
    {
        // 从data中获取formId、退回原因以及退回原因详细描述
        int formID = (int) ((LinkedHashMap)data.get("items")).get("formID");
        String reason = (String) ((LinkedHashMap)data.get("comments")).get("reason");
        String reasonDescription = (String) ((LinkedHashMap)data.get("comments")).get("otherReasonDescription");
        boolean success = dataFormExamineService.returnBackForm(formID, reason, reasonDescription);
        if (success) {
            return Result.success();
        }
        return Result.fail();
    }
}
